// 主入口文件
import Vue from "vue";
import app from "./App.vue";

import router from "./router.js";
//import Mint from 'mint-ui'
//import { Header,Swipe, SwipeItem , Button,Lazyload } from 'mint-ui';
import "./lib/mui/css/mui.min.css";
import "./lib/mui/css/icons-extra.css";
// Vue.component( 'mt-header',Header);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
//全部mnit-ui加载
import MintuI from "mint-ui"
import Vueresource from "vue-resource";
import moment from "moment";
import VuePreview from 'vue-preview';
// // defalut install
 Vue.use(VuePreview);

Vue.use(MintuI);

//Vue.use(Mint)

Vue.use( Vueresource );
Vue.http.options.root="http://www.liulongbin.top:3005/";
//统一设置请求方式的参
Vue.http.options.emulateJSON =true;
//parse将数字的字符串表示形式转换为它的等效 32 位有符号整数。
var cars=JSON.parse(localStorage.getItem("car") || "[]")


import Vuex from 'vuex';

Vue.use(Vuex);
var store = new Vuex.Store({
  state:{
  car:cars, 
  },
  mutations:{
   // this.$store.commit("名字","唯一的参数")
   add_ture(state,goood){
     //进行两次对比
     var fiag=false;
     state.car.some(itemx=>{
         if(itemx.id==goood.id){
          itemx.count += goood.count;//本身加上刚刚摁的
          fiag = true;
          return true;
         }
       })
       
    // state.car.push(goood);
    if(!fiag){
      state.car.push(goood);
    }
//暂存在本地 localStorage中
    localStorage.setItem("car",JSON.stringify(state.car));
    // console.log(state.car);
    
   },
   uodateGoodsinfo(state,Oood){
    state.car.some(iems=>{
    if(iems.id==Oood.id) {
       iems.count= parseInt (Oood.count);
       return true ;
     }
    })
    
    localStorage.setItem("car",JSON.stringify(state.car));
    //  console.log(state.car);
    
     
   },

   removeFromc(state,id){
     state.car.some((items,i)=>{
       if(items.id==id){
        state.car.splice(i,1);
        return true
       }
     }),
     localStorage.setItem("car",JSON.stringify(state.car));

   },
   selecteds(state,obj){
    state.car.some(items=>{
     if(items.id==obj.id){
      items.selected=obj.selected
     }
     })
     localStorage.setItem("car",JSON.stringify(state.car));

  },
},

    getters:{ 
     gettets(state){
      var c=0;
      state.car.forEach(iems=>{
        //parseInt
        c += parseInt(iems.count)
      })
      return c;
    },
    getOgood(state){
      var o={};
      state.car.forEach(items=>{
        o[items.id] = items.count;

      })
    //  console.log("dong");
     
      
      return o;

    },
    // getGoodsSelected(state){
    //   var a={};
    //   state.car.some(items=>{
    //     a[items,id]=items.selected;
    //   })
    //   return a;
    // }
    getGoodsSelected(state){
      var a={};
      state.car.some(items =>{
        a[items.id]=items.selected;
      })
      return a;
    },
    // getAll(state){
    //   var q={
    //   count:0,
    //   amount:0,
    //   }
    //   state.car.forEach(items => {
    //     if(items.selected){
    //       q.count += parseInt(items.count);
    //       q.amount += items.pcice*items.count;
    //     }
    //   })
    //   return q;
    // }
    getAll(state){
    var all={
      count:0,//勾选的数量
      amount:0,//勾选的总价
   }
  //  console.log(state.car);
   
  state.car.forEach(items =>{
      if(items.selectle){
        console.log("---"+items.price);
        
          all.count += parseInt(items.count);
          all.amount += items.price * items.count;

      }
  })
  // console.log(all);
  
  return all;
}
  }
})


Vue.filter("binia", function(datastr ,parttern="YYYY-MM-DD HH:MM:SS"){
return moment(datastr).format(parttern);

})

new Vue({
    el:"#app",
    router,
  render:c=> c(app),
  store,
  handleClick: function() {
    this.$toast('Hello world!')}
})